package context;

import context.environ.Environ;
import context.environ.Keys;
import context.environ.Window;
import java.util.LinkedList;
import math.rand.RNG;

public class Context
{
    
    public static final RNG RNG = new RNG(System.nanoTime());
    
    public static boolean IS_RUNNING = false;
    
    
    private Environ mEnviron;
    
    private LinkedList<ContextThread> mLContThreads;
    
    
    public Context()
    {
        this.mEnviron = new Environ();
        
        this.mLContThreads = new LinkedList<>();
    }
    
    
    public void addThread(ContextThread aThread)
    {
        this.mLContThreads.add(aThread);
    }
    
    public void run()
    {
        Context.IS_RUNNING = true;
        
        while (Context.IS_RUNNING)
        {
            this.mEnviron.onTick();
            
            for (ContextThread thread : this.mLContThreads)
            {
                thread.onTick();
            }
            
            if (Window.get().isCloseRequested() || Keys.get().isReleased(Keys.ESCAPE))
            {
                Context.IS_RUNNING = false;
            }
        }
        
        for (ContextThread thread : this.mLContThreads)
        {
            thread.onEnd();
        }
        
        this.mEnviron.onEnd();
    }
    
}
